package com.whmall.service.crm.util;

import org.springframework.util.Assert;

/**
 * 字符串Bitmap工具类
 * <p>
 * 如果某序号对应值为1，则表示该序号不生效
 * 如果某序号对应值为0或不存在，则表示该序号生效
 * </p>
 *
 * @author alex
 * @since 2020/8/3
 */
public abstract class StringBitmapUtils {
    private static final char TRUE = '0';
    private static final char FALSE = '1';

    public static boolean isTrue(String bitmap, Integer index) {
        Assert.isTrue(index >= 0, "index不能小于0");
        Assert.notNull(bitmap, "bitmap不能为null");

        if (index >= bitmap.length()) {
            return true;
        }

        return bitmap.charAt(index) == TRUE;
    }

    public static String asFalse(String bitmap, Integer index) {
        Assert.isTrue(index >= 0, "index不能小于0");
        Assert.notNull(bitmap, "bitmap不能为null");

        StringBuilder builder = new StringBuilder();
        builder.append(bitmap);
        if (index >= bitmap.length()) {
            for (int i = bitmap.length(); i < index; i++) {
                builder.append(TRUE);
            }
            builder.append(FALSE);
            return builder.toString();
        }

        builder.setCharAt(index, FALSE);
        return builder.toString();
    }

    public static String asTrue(String bitmap, Integer index) {
        Assert.isTrue(index >= 0, "index不能小于0");
        Assert.notNull(bitmap, "bitmap不能为null");
        if (index >= bitmap.length()) {
            return bitmap;
        }

        StringBuilder builder = new StringBuilder();
        builder.append(bitmap);
        builder.setCharAt(index, TRUE);
        removeUnnecessaryTailForTure(builder);
        return builder.toString();
    }

    public static String or(String b1, String b2) {
        Assert.notNull(b1, "b1不能为null");
        Assert.notNull(b2, "b2不能为null");

        if (b1.equals(b2)) {
            return b1;
        }

        String c1 = b1.length() < b2.length() ? b1 : b2;
        String c2 = c1.equals(b1) ? b2 : b1;

        StringBuilder builder = new StringBuilder(c1.length());
        for (int i = 0, len = c1.length(); i < len; i++) {
            if (isTrue(c1, i) || isTrue(c2, i)) {
                builder.append(TRUE);
            } else {
                builder.append(FALSE);
            }
        }

        removeUnnecessaryTailForTure(builder);
        return builder.toString();
    }

    public static String and(String b1, String b2) {
        Assert.notNull(b1, "b1不能为null");
        Assert.notNull(b2, "b2不能为null");

        String c1 = b1.length() > b2.length() ? b1 : b2;
        String c2 = c1.equals(b1) ? b2 : b1;

        StringBuilder builder = new StringBuilder(c1.length());
        for (int i = 0, len = c1.length(); i < len; i++) {
            if (isTrue(c1, i) && isTrue(c2, i)) {
                builder.append(TRUE);
            } else {
                builder.append(FALSE);
            }
        }

        removeUnnecessaryTailForTure(builder);
        return builder.toString();
    }

    public static String removeUnnecessaryTailForTure(String bitmap) {
        StringBuilder builder = new StringBuilder(bitmap);
        removeUnnecessaryTailForTure(builder);
        return builder.toString();
    }

    private static void removeUnnecessaryTailForTure(StringBuilder builder) {
        while (builder.length() > 0 &&
                builder.charAt(builder.length() - 1) == TRUE) {
            builder.deleteCharAt(builder.length() - 1);
        }
    }
}
